//
//  DUButton.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/21.
//

import Cocoa

@IBDesignable
class DUImgButton: BaseView {

    var imageView:NSImageView!
    
    
    convenience init() {
        self.init(frame: CGRect.zero)
    }

    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        initialize()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        initialize()
    }
    
    private func initialize () {
        self.mouseDownStyle = getDownStyle()
        self.mouseEnteredStyle = getEnteredStyle()
        self.defaultStyle = ViewStyleUtil.dayDefaultStyle()
        imageView = NSImageView()
        imageView.frame = NSRect(x: 5, y: 5, width: 30, height: 30)
        addSubview(imageView)
        
    }
    
    override func layout() {
        let w = self.frame.width
        let h = self.frame.height
        let x:CGFloat = w > 30 ? 5 : 2
        imageView.frame = NSRect(x: x, y: x, width: (w - (2 * x)), height: (h - (2 * x))) //NSMakeRect(x, x, w - x - x, h - x - x)
    }
    
    @IBInspectable var image:NSImage? {
        didSet{
            self.imageView.image = image
        }
    }
    
    private func getDownStyle() -> ViewStyle {
        var s = ViewStyle()
        s.backgroundColor = CGColor(red: 255, green: 255, blue: 255, alpha: 0.4)
        s.borderColor = nil
        s.borderWidth = 0
        s.radius = 8
        return s
    }
    
    private func getEnteredStyle() -> ViewStyle {
        var s = ViewStyle()
        s.backgroundColor = CGColor(red: 255, green: 255, blue: 255, alpha: 0.2)
        s.borderColor = nil
        s.borderWidth = 0
        s.radius = 8
        return s
    }
}
